import { Component, OnInit, ViewChild } from '@angular/core';
import { PhotoService } from '../../service/photo.service';

@Component({
    selector: 'app-photo-login',
    templateUrl: './list.component.html',
    styleUrls: ['./list.component.scss']
})
export class LoginListComponent implements OnInit {
    @ViewChild('grid') grid;

    loginInfos: any[];
    pageNumber: number = 1;
    pageSize: number = 8;
    totalPage: number = 0;
    searchParam: string = '';
    isReady: boolean;

    constructor(
        private photoService: PhotoService
    ) { };

    ngOnInit() {
        this.initGrid(1);
    };

    // 上一页
    prevPage() {
        this.pageNumber = this.pageNumber - 1;
        this.clear();
        this.initGrid(0);
    };

    // 下一页
    nextPage() {
        this.pageNumber = this.pageNumber + 1;
        this.clear();
        this.initGrid(0);
    };

    // 表格构建
    initGrid(status) {
        let grid = this.grid.nativeElement;
        this.photoService.userLogin(
            this.pageNumber.toString(),
            this.pageSize.toString(),
            this.searchParam
        ).then(result => {
            if (result.code === '10000') {
                this.loginInfos = result.data.list;
                if(status){
                    this.totalPage = Math.ceil(+result.data.totalPage / this.pageSize);
                };
                grid.columns[2].renderer = cell => {
                    cell.data ?
                        cell.element.innerHTML = '<i class="uk-icon-check uk-text-success"></i>' :
                        cell.element.innerHTML = '<i class="uk-icon-close uk-text-danger"></i>';
                };
                this.isReady = true;
            };
        });
    };

    clear(){
        this.loginInfos = [];
        this.isReady = false;
    };
}
